package com.kuaifawu.lwnlawyerclient.Lib;

import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.kuaifawu.lwnlawyerclient.Model.LWNModel_comment;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_lawyerInfo;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_message;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_messageSystem;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_myGoodAtList;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_myQuestion;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_questionAdd;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_questionDetail;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Json解析类
 * Created by zhangminglei on 15/1/15.
 */
public class LWNJsonToData {

    /**
     * @param jsonArray 获取我擅长的问题列表
     * @return
     */
    public static ArrayList<LWNModel_myGoodAtList> getGoodAtListWithJsonString(JSONArray jsonArray){
        ArrayList<LWNModel_myGoodAtList> array_result = new ArrayList<LWNModel_myGoodAtList>();
        try {
            for (int i = 0; i<jsonArray.length(); i++) {

                JSONObject object = jsonArray.getJSONObject(i);
                LWNModel_myGoodAtList model_myGoodAtListl = new LWNModel_myGoodAtList();
                model_myGoodAtListl.setId(object.getString("id"));
                ArrayList<String> array_img = new ArrayList<String>();
                try {
                    JSONArray array_img_th = object.getJSONArray("img_thumbnail");
                    for (int j = 0; j < array_img_th.length(); j++) {
                        String string_img = array_img_th.getString(j);
                        //System.out.println(string_img);
                        array_img.add(string_img);
                    }
                }
                catch (Exception f){
                    array_img = null;
                }

                model_myGoodAtListl.setImg_thumbnail(array_img);
                model_myGoodAtListl.setQuestion(object.getString("question"));
                model_myGoodAtListl.setQuestion_area(object.getString("question_area"));
                model_myGoodAtListl.setQuestion_type(object.getString("question_type"));
                model_myGoodAtListl.setTime(object.getString("time"));

                ArrayList<String> array_big = new ArrayList<String>();
                try{
                    JSONArray array_img_big = object.getJSONArray("question_img");
                    for (int k = 0; k < array_img_big.length(); k++) {
                        String string_img = array_img_big.getString(k);
                        array_big.add(string_img);
                    }
                }catch (Exception e){
                    array_big = null;
                }
                model_myGoodAtListl.setQuestion_img(array_big);
                array_result.add(model_myGoodAtListl);
            }
        } catch (Exception e) {
            System.out.println("error");
        }
        return array_result;
    }

    /**
     * @param jsonArray 获取我已回答的问题列表
     * @return
     */
    public static ArrayList<LWNModel_myQuestion> getMyQuestionWithJsonString(JSONArray jsonArray) {
        ArrayList<LWNModel_myQuestion> array_result = new ArrayList<LWNModel_myQuestion>();
        try {
            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject object = jsonArray.getJSONObject(i);
                LWNModel_myQuestion model_myquestion = new LWNModel_myQuestion();
                model_myquestion.setId(object.getString("id"));
                model_myquestion.setIs_solve(object.getString("is_solve"));

//                ArrayList<String> array_img = new ArrayList<String>();
//                try {
//                    JSONArray array_img_th = object.getJSONArray("img_thumbnail");
//                    for (int j = 0; j < array_img_th.length(); j++) {
//                        String string_img = array_img_th.getString(j);
//                        //System.out.println(string_img);
//                        array_img.add(string_img);
//                    }
//                } catch (Exception f) {
//                    array_img = null;
//                }
//                model_myquestion.setImg_thumbnail(array_img);

                model_myquestion.setQuestion(object.getString("question"));
                model_myquestion.setQuestion_area(object.getString("question_area"));
                model_myquestion.setQuestion_type(object.getString("question_type"));
                model_myquestion.setTime(object.getString("time"));

//                ArrayList<String> array_big = new ArrayList<String>();
//                try {
//                    JSONArray array_img_big = object.getJSONArray("question_img");
//                    for (int k = 0; k < array_img_big.length(); k++) {
//                        String string_img = array_img_big.getString(k);
//                        array_big.add(string_img);
//                    }
//                } catch (Exception e) {
//                    array_big = null;
//                }
//                model_myquestion.setQuestion_img(array_big);
                  array_result.add(model_myquestion);
            }
        } catch (Exception e) {
            //System.out.println("error");
        }
        return array_result;
    }

    /**
     * @param jsonObject 获取问题详情
     * @return
     */
    public static LWNModel_questionDetail getQuestionDetail(JSONObject jsonObject) {
        LWNModel_questionDetail model_questionDetail = new LWNModel_questionDetail();
        try {
            model_questionDetail.setId(jsonObject.getString("id"));
            try {
                JSONArray array_img_th = jsonObject.getJSONArray("img_thumbnail");
                ArrayList <String> array_img = new ArrayList<String>();
                for (int i = 0; i < array_img_th.length(); i++) {
                    String string_img = array_img_th.getString(i);
                    array_img.add(string_img);
                }
                model_questionDetail.setImg_thumbnai(array_img);
            } catch (Exception f) {

            }
            model_questionDetail.setIslawyansw(jsonObject.getString("islawyansw"));
            model_questionDetail.setQuestion(jsonObject.getString("question"));
            model_questionDetail.setQuestion_area(jsonObject.getString("question_area"));
            model_questionDetail.setTime(jsonObject.getString("time"));
            try {
                JSONArray array_img_big_tmp = jsonObject.getJSONArray("question_img");
                ArrayList<String> array_img_big = new ArrayList<String>();
                for (int j = 0; j < array_img_big_tmp.length(); j++) {
                    String string_img = array_img_big_tmp.getString(j);
                    array_img_big.add(string_img);
                }
                model_questionDetail.setQuestion_img(array_img_big);
            } catch (Exception f) {

            }
            try {
                JSONArray array_add = jsonObject.getJSONArray("questiondetails");
                ArrayList<LWNModel_questionAdd> array_questionadd = new ArrayList<LWNModel_questionAdd>();
                for (int k = 0; k < array_add.length(); k++) {
                    ArrayList<String> img_thum=new ArrayList<String>();
                    ArrayList<String> img_contentadd=new ArrayList<String>();
                    LWNModel_questionAdd model_add = new LWNModel_questionAdd();
                    JSONObject model_addquestion = array_add.getJSONObject(k);
                    model_add.setId(model_addquestion.getString("id"));
                    model_add.setType(model_addquestion.getString("type"));
                    int type = Integer.parseInt(model_add.getType());
                    model_add.setContent(model_addquestion.getString("content"));
                    JSONArray img_thumarray = model_addquestion.optJSONArray("img_thumbnail");
                    if(img_thumarray!=null){
                        for(int m = 0; m < img_thumarray.length(); m++){
                            img_thum.add(img_thumarray.getString(m));

                        }
                        model_add.setImg_thum(img_thum);
                    }
                    JSONArray contentadd_imgarray = model_addquestion.optJSONArray("content_img");
                    if(contentadd_imgarray!=null){
                        for(int n = 0; n < contentadd_imgarray.length(); n++){
                            img_contentadd.add(contentadd_imgarray.getString(n));

                        }
                        model_add.setContentadd_img(img_contentadd);
                    }
                    if(type == 0) {
                        if (model_addquestion.getString("head_src") != null) {
                            model_add.setHead_src(model_addquestion.getString("head_src"));
                        }
                        model_add.setRealname(model_addquestion.getString("realname"));
                    }
                    model_add.setTime(model_addquestion.getString("time"));
                    array_questionadd.add(model_add);
                }
                model_questionDetail.setArray_questionadd(array_questionadd);
            } catch (Exception f) {
               f.printStackTrace();

            }
            try{
                JSONObject json_comment = jsonObject.getJSONObject("comment");
                if (json_comment != null) {
                    LWNModel_comment model_comment = new LWNModel_comment();
                    model_comment.setAutomatic_adopt(json_comment.getString("automatic_adopt"));
                    model_comment.setComment_time(json_comment.getString("comment_time"));
                    model_comment.setContent(json_comment.getString("content"));
                    model_comment.setFuwu_fraction(json_comment.getString("fuwu_fraction"));
                    model_comment.setXy_fraction(json_comment.getString("xy_fraction"));
                    model_comment.setZy_fraction(json_comment.getString("zy_fraction"));
                    model_questionDetail.setComment(model_comment);
                }
            }catch (Exception g){

            }

        } catch (Exception e) {

        }
        return model_questionDetail;
    }

    /**
     * @param jsonArray 获取消息动态列表
     * @return
     */
    public static ArrayList<LWNModel_message> getDongtaiWithJsonString(JSONArray jsonArray) {
        ArrayList<LWNModel_message> array_result = new ArrayList<LWNModel_message>();

        try {
            for (int i = 0; i < jsonArray.length(); i++) {
                ArrayList<String> array_image = new ArrayList<String>();
                JSONObject object = jsonArray.getJSONObject(i);
                LWNModel_message model_myquestion = new LWNModel_message();
                model_myquestion.setId(object.getString("id"));
                model_myquestion.setFlag(object.getInt("flag"));
                model_myquestion.setContent(object.getString("content"));
                model_myquestion.setIsunread(object.getString("isunread"));
                model_myquestion.setQuestion_area(object.getString("question_area"));
                model_myquestion.setQuestion_type(object.getString("question_type"));
                model_myquestion.setQuestion_id(object.getString("question_id"));
                model_myquestion.setTime(object.getString("time"));
                JSONArray imgArray= object.optJSONArray("contentimg");
                if(imgArray!=null){
                    for (int j=0;j<imgArray.length();j++){
                        array_image.add(imgArray.getString(j));
                    }
                }

                model_myquestion.setArray_image(array_image);
                try {
                    model_myquestion.setFw_fraction(object.getString("fw_fraction"));
                    model_myquestion.setXy_fraction(object.getString("xy_fraction"));
                    model_myquestion.setZy_fraction(object.getString("zy_fraction"));
                }
                catch (Exception f){

                }
                array_result.add(model_myquestion);
            }
        } catch (Exception e) {
            System.out.println("error");
        }
        return array_result;
    }


    /**
     * @param jsonArray 获取系统消息列表
     * @return
     */
    public static ArrayList<LWNModel_messageSystem> getSystemWithJsonString(JSONArray jsonArray) {
        ArrayList<LWNModel_messageSystem> array_result = new ArrayList<LWNModel_messageSystem>();
        try {
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                LWNModel_messageSystem model_myquestion = new LWNModel_messageSystem();
                model_myquestion.setAutomatic(object.getString("automatic"));
                model_myquestion.setContent(object.getString("content"));
                model_myquestion.setContentimg(object.getString("contentimg"));
                model_myquestion.setId(object.getString("id"));
                model_myquestion.setIsunread(object.getString("isunread"));
                model_myquestion.setLawyid(object.getString("lawyid"));
                model_myquestion.setTime(object.getString("time"));
                model_myquestion.setLink(object.getString("link"));
                model_myquestion.setMedaltype(object.getString("medaltype"));
                model_myquestion.setTitle(object.getString("title"));
                array_result.add(model_myquestion);
            }
        } catch (Exception e) {
            //System.out.println("error");
        }
        return array_result;
    }

    /**
     * @param @jsonArray 存储律师信息
     * @return
     */
    public static LWNModel_lawyerInfo saveLawyerInfoWithJsonString(JSONObject object_lawyer) {
        try {
                LWNModel_lawyerInfo lawyerInfo = LWNModel_lawyerInfo.getInstance();
                lawyerInfo.setId(object_lawyer.getString("id"));
                lawyerInfo.setArea(object_lawyer.getString("area"));
                lawyerInfo.setEducation(object_lawyer.getString("education"));
                lawyerInfo.setEvaluation(String.valueOf(object_lawyer.getInt("evaluation")));
                lawyerInfo.setGoodatfield(object_lawyer.getString("goodatfield"));
                lawyerInfo.setHead_src(object_lawyer.getString("head_src"));
                lawyerInfo.setIntegral(object_lawyer.getString("integral"));
                lawyerInfo.setLabel(object_lawyer.getString("label"));
                lawyerInfo.setIntro(object_lawyer.getString("intro"));
                lawyerInfo.setLife(object_lawyer.getString("life"));
                lawyerInfo.setMechanism(object_lawyer.getString("mechanism"));
                lawyerInfo.setOffice_address(object_lawyer.getString("office_address"));
                lawyerInfo.setPosition(object_lawyer.getString("position"));
                lawyerInfo.setQualificati(object_lawyer.getString("qualificati"));
                lawyerInfo.setRealname(object_lawyer.getString("realname"));
                lawyerInfo.setString_class(object_lawyer.getString("class"));
                lawyerInfo.setSignatu(object_lawyer.getString("signatu"));
                lawyerInfo.setSolvecount(String.valueOf(object_lawyer.getInt("solvecount")));
                lawyerInfo.setShanchang(object_lawyer.getString("shanchang"));
            return lawyerInfo;
        } catch (Exception e) {
            //System.out.println("error");
        }
        return null;
    }
}
